<?php

namespace app\command;

use app\model\table\season\SeasonConfig;
use app\model\table\season\SeasonScoreLog;
use app\model\table\user\User;
use think\console\Command;
use think\console\Input;
use think\console\Output;


/**
 * @title 定时任务
 */
class commandRankSeason extends Command
{
    protected function configure()
    {
        $this->setName('commandRankSeason')->setDescription("计划任务 commandRankSeason");//
    }

    /**
     *   工会赛季排行榜
     */
    protected function execute(Input $input, Output $output)
    {
        // 首先获取当前赛季
        $SeasonObj = SeasonConfig::instance()->getNow();
        $season_id = $SeasonObj['id'];

        // 获取这个赛季的前三名到数据库
        $where['limit'] = 3;
        $where['orderBy'] = "score_season desc";
        $objs = User::instance()->findMore($where);

        SeasonScoreLog::instance()->delMore(['season_id'=>$season_id]);
        $rs = array();
        foreach ($objs as $key=>$val){
            $rs[] = array(
                'user_id' => $val['id'],
                'ranking' => $key+1,
                'season_id' => $season_id,
                'score' => $val['score_season'],
                'created_at' => date("Y-m-d H:i:s", time()),
            );
        }
        SeasonScoreLog::instance()->insertMore($rs);
        $output->writeln('success');
    }
}
